import {createRouter, createWebHistory} from "vue-router";
import {getCookies, msg} from "@ut/tool.js";

let router = createRouter({
    history: createWebHistory(),
    routes: [
        {path: '/', redirect: '/index'},
        {
            path: '/index',
            redirect: '/index/home',
            component: () => import('@pa/Index.vue'),
            children: [
                {path: 'home', name: '首页', component: () => import('@pa/index/Home.vue')},
                {path: 'chamber', name: '宿舍管理', component: () => import('@pa/index/Chamber.vue')},
                {path: 'student', name: '学生管理', component: () => import('@pa/index/Student.vue')},
                {path: 'ca', name: '宿管员管理', component: () => import('@pa/index/ChamberAdmin.vue')},
                {path: 'notice', name: '公告管理', component: () => import('@pa/index/Notice.vue')},
                {path: 'task', name: '考勤管理', component: () => import('@pa/index/Task.vue')},
                {path: 'task_do', name: '考勤打卡', component: () => import('@pa/index/TaskUser.vue')},
                {path: 'upkeep', name: '维修管理', component: () => import('@pa/index/Upkeep.vue')},
                {path: 'health', name: '卫生管理', component: () => import('@pa/index/Health.vue')},
            ]
        },
        {path: '/login', component: () => import('@pa/Login.vue')},
        {path: '/:pathMatch(.*)*', redirect: '/404'},
        {path: '/404', name: '404', component: () => import('@pa/404.vue')},
    ]
});

router.beforeEach(async (to, from, next) => {
    if (getCookies('userInfo')) {
        next();
    } else {
        if (to.path === '/login') next();
        else {
            msg('登录已过期，请重新登录', 'warning');
            next('/login');
        }
    }
})

export default router;